package com.ruoyi.web.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictTypeService;

/**
 * 小程序字典数据信息接口
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/app/dict")
public class AppDictController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @Anonymous
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (data == null)
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }
    
    /**
     * 根据字典类型查询字典数据信息（限制数量）
     */
    @Anonymous
    @GetMapping(value = "/type/{dictType}/limit/{limit}")
    public AjaxResult dictTypeLimit(@PathVariable String dictType, @PathVariable int limit)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (data == null)
        {
            data = new ArrayList<SysDictData>();
        }
        
        // 限制返回数量
        if (data.size() > limit) {
            return success(data.subList(0, limit));
        }
        
        return success(data);
    }
    
    /**
     * 获取资产类型分类数据（用于首页展示）
     */
    @Anonymous
    @GetMapping(value = "/home/categories")
    public AjaxResult homeCategories(@RequestParam(defaultValue = "5") int limit)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType("asset_type");
        if (data == null)
        {
            data = new ArrayList<SysDictData>();
        }
        
        // 限制返回数量
        if (data.size() > limit) {
            return success(data.subList(0, limit));
        }
        
        return success(data);
    }
} 